package com.example.dubbo.simple;

import com.example.dubbo.api.model.User;
import com.example.dubbo.api.service.UserService;
import org.apache.dubbo.config.*;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 简化的 Dubbo Provider 示例（纯 Java，无 Spring Boot）
 * 
 * 编译运行：
 * javac -cp "dubbo-api.jar:dubbo-3.2.9.jar:nacos-client-2.3.0.jar:..." SimpleDubboProvider.java
 * java -cp ".:dubbo-api.jar:dubbo-3.2.9.jar:..." SimpleDubboProvider
 */
public class SimpleDubboProvider {
    
    public static void main(String[] args) throws Exception {
        // 1. 服务实现
        UserService userService = new UserService() {
            private Map<Long, User> users = new ConcurrentHashMap<>();
            
            {
                users.put(1L, new User(1L, "张三", "zhang@example.com", 25));
                users.put(2L, new User(2L, "李四", "li@example.com", 30));
            }
            
            @Override
            public User getUserById(Long id) {
                System.out.println("Provider: getUserById(" + id + ")");
                return users.get(id);
            }
            
            @Override
            public List<User> getAllUsers() {
                System.out.println("Provider: getAllUsers()");
                return new ArrayList<>(users.values());
            }
            
            @Override
            public User createUser(User user) {
                user.setId((long) (users.size() + 1));
                users.put(user.getId(), user);
                return user;
            }
            
            @Override
            public Boolean deleteUser(Long id) {
                return users.remove(id) != null;
            }
        };
        
        // 2. 应用配置
        ApplicationConfig application = new ApplicationConfig();
        application.setName("simple-dubbo-provider");
        
        // 3. 注册中心配置（只使用 Nacos 注册中心）
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("nacos://127.0.0.1:8848");
        registry.setSimplified(true);  // 简化模式
        
        // 4. 协议配置
        ProtocolConfig protocol = new ProtocolConfig();
        protocol.setName("dubbo");
        protocol.setPort(20880);
        
        // 5. 服务配置
        ServiceConfig<UserService> service = new ServiceConfig<>();
        service.setInterface(UserService.class);
        service.setRef(userService);
        service.setVersion("1.0.0");
        
        // 6. 启动 Dubbo
        DubboBootstrap.getInstance()
                .application(application)
                .registry(registry)
                .protocol(protocol)
                .service(service)
                .start();
        
        System.out.println("========================================");
        System.out.println("  Simple Dubbo Provider 启动成功！");
        System.out.println("  Protocol: dubbo");
        System.out.println("  Port: 20880");
        System.out.println("  Registry: nacos://127.0.0.1:8848");
        System.out.println("========================================");
        
        // 保持运行
        System.in.read();
    }
}

